﻿using System.Globalization;
using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentInfo : Component
    {
        public AttackType attackType;

        public bool Hired;

        public string TamedValue;

        public int Level;

        public float Defence;

        public void LevelUp(int level)
        {
            Level = MathUtils.Max(0, Level + level);
        }

        public int DecodeValue(string result)
        {
            string[] array = result.Split(':');
            Block block = BlocksManager.FindBlockByTypeName(array[0], throwIfNotFound: true);
            int data = ((array.Length >= 2) ? int.Parse(array[1], CultureInfo.InvariantCulture) : 0);
            return Terrain.MakeBlockValue(block.BlockIndex, 0, data);
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            attackType = valuesDictionary.GetValue<AttackType>("attackType");
            Hired = valuesDictionary.GetValue<bool>("Hired");
            TamedValue = valuesDictionary.GetValue<string>("TamedValue");
            Level = valuesDictionary.GetValue<int>("Level");
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("Hired", Hired);
            valuesDictionary.SetValue("Level", Level);
        }
    }
}